/* 
 * File:   TimeClustering.cpp
 * Author: Claudio
 * 
 * Created on February 26, 2013, 1:54 AM
 */

#include "TimeClustering.h"
#include <iostream>
using namespace std;
namespace AAM{
TimeClustering::TimeClustering() {
}

TimeClustering::~TimeClustering() {
}
bool TimeClustering::comparaHoraImagens(Imagens* a, Imagens *b){
    return a->getTime() < b->getTime();
}
//int TimeClustering::howManyTimeClusters(vector<Imagens*> allImages){
//    sort(allImages.begin(),allImages.end(),TimeClustering::comparaHoraImagens);
//    //Search for the outlier
//    int q1pos = vec.size()/4;
//    int q3pos = q1pos*3;
//    double q1 = vec.at(q1pos);
//    double q3 = vec.at(q3pos);
//    double iqr = q3-q1;
//    double outlierThreshold = q1-1.5*iqr;
//
//}
//vector<Event> TimeClustering::doCluster(vector<Imagens*> &allImages){
TimeBlock* TimeClustering::doCluster(vector<Imagens*> &allImages){
    sort(allImages.begin(),allImages.end(),TimeClustering::comparaHoraImagens);
//    vector<double> timeDiffs;
//    vector<pair<double,int> > timeDiffs;
//    ofstream out1("saidaHoras.txt",ios_base::trunc);
//    int starting=0;
//    if(allImages[0]->getTime()==-1){
//        //Oh no! There are invalid timestamps. Nothing to do. Put then together. 
//        int b=0;
//        while(allImages[b]->getTime()==-1){
//            b++;
//        }
//        starting = b;
//    }
//    for(int a=starting;a<allImages.size();a++){
//        out1 << endl << allImages[a]->getTime();
//        if(allImages[a]->getTime()==-1){
//            //NO timestamp in exif.
//        }
//        if(a>starting){
//            double timeDiffVal = -log10((allImages[a]->getTime()-allImages[a-1]->getTime())+1);
//            pair<double,int> p;
//            p.first = timeDiffVal;
//            p.second = a;
//            timeDiffs.push_back(p);
//            out1 << " " << timeDiffVal << " " << allImages[a]->getFilename();
//        }
//    }
//    out1.close();
//    vector<Event> ev = searchForEvents(timeDiffs,starting);
//    if(starting!=0 && ev.size()!=0){
//        Event error(0,starting);
//        ev.push_back(error);
//    }
//    
    TimeBlock *tb = new TimeBlock(allImages.begin(),allImages.end());
    
//    vector<Event> ev;
//    searchForEvents(tb,ev);
////	for(int a=0;a<ev.size();a++)
//	vector<Event>::iterator evit = ev.begin();
//	int counter = 0;
//	while(evit!=ev.end()){
//		vector<AAM::Imagens*>::iterator imit = (*evit).getItBegin();
//		while(imit!=(*evit).getItEnd()){
//			cout << endl << counter << " -> " <<  (*imit)->getFilename();
//			imit++;
//		}
//		
//				
//		counter++;
//		evit++;
//	}
//    return ev;
    return tb;
}
void TimeClustering::searchForEvents(TimeBlock* t,vector<Event> &ev){
    vector<TimeBlock*> subblocks = t->getSubBlocks();
    if(subblocks.size()==0){
        Event e(t->getBegin(),t->getEnd());
        ev.push_back(e);
    }else{
        for(int a=0;a<subblocks.size();a++){
			
            searchForEvents(subblocks[a],ev);
        }
    }
    
}
TimeBlock* TimeClustering::timeBlockClustering(vector<Imagens*> &allImages){
    sort(allImages.begin(),allImages.end(),TimeClustering::comparaHoraImagens);
    TimeBlock *tb = new TimeBlock(allImages.begin(),allImages.end());
    return tb;
}
}
